//
//  XGUserDetailNavView.swift
//  TodayNews
//
//  Created by monkey on 2020/2/22.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit

protocol XGUserDetailNavViewDelegate: AnyObject
{
    func goBackButtonClick() -> Void
    func titleItemDidSelected(index: Int) -> Void
}

class XGUserDetailNavView: UIView
{
    /// 代理
    weak var delegate: XGUserDetailNavViewDelegate?
    /// 标题数组
    var titles: [String]? {
        didSet {
            guard let titles = titles else {
                return
            }
            
            let width = ceil(scrollView.width / 6.0)
            for (index,str) in titles.enumerated() {
                let button = UIButton(title: str, fontSize: 15, normalColor: UIColor.darkGray, target: self, action: #selector(titleSelectedAction(button:)))
                button.tag = index
                button.setTitleColor(UIColor.red, for: .selected)
                scrollView.addSubview(button)
                button.frame = CGRect(x: CGFloat(index) * width, y: 0, width: width, height: scrollView.height)
                if index == 0 {
                    button.isSelected = true
                    previousSelectedButton = button
                }
            }
            scrollView.contentSize = CGSize(width: width * CGFloat(titles.count), height: 0)
        }
    }
    
    /// 选中某个标题
    func selectItemAt(_ index: Int) -> Void
    {
        guard let button = scrollView.subviews[index] as? UIButton else {
            return
        }
        
        previousSelectedButton?.isSelected = false
        button.isSelected = true
        previousSelectedButton = button
    }
    
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews()
    {
        super.layoutSubviews()
        
        backButton.frame = CGRect(x: 10, y: bounds.height - 44, width: 44, height: 44)
        lineView.frame = CGRect(x: 0, y: bounds.height - 0.5, width: bounds.width, height: 0.5)
        scrollView.frame = CGRect(x: backButton.frame.maxX + 10, y: backButton.y, width: bounds.width - 10 - backButton.frame.maxX - 10, height: 44)
    }
    
    /// 返回按钮
    private lazy var backButton: UIButton = UIButton(imageName: "personal_home_back_black_24x24_", target: self, action: #selector(goBackAction))
    /// 分割线
    private lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.lightGray
        return view
    }()
    /// 滚动视图
    private lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.backgroundColor = UIColor.white
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        return scrollView
    }()
    /// 上一个选中的按钮
    private weak var previousSelectedButton: UIButton?
}

// MARK: - 私有扩展

private extension XGUserDetailNavView
{
    /// 标题选中事件
    @objc func titleSelectedAction(button: UIButton) -> Void
    {
        previousSelectedButton?.isSelected = false
        button.isSelected = true
        previousSelectedButton = button
        delegate?.titleItemDidSelected(index: button.tag)
    }
    
    /// 返回事件
    @objc func goBackAction() -> Void
    {
        delegate?.goBackButtonClick()
    }
    
    func setUpUI() -> Void
    {
        backgroundColor = UIColor.white
        addSubview(backButton)
        addSubview(scrollView)
        addSubview(lineView)
    }
}
